package com.pansoft.openplanet.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.activity.BaseActivity;
import com.efounder.chat.zxing.qrcode.QRCode;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.TransactionBean;
import com.pansoft.openplanet.util.EosNetParamUtil;
import com.utilcode.util.StringUtils;
import com.utilcode.util.ToastUtils;

import static com.pansoft.openplanet.activity.TradeLineActivity.TRANSACTION_TYPE_OUT;

/**
 * 交易明细详情
 */
public class TransactionDetailActivity extends BaseActivity implements View.OnClickListener{

    private ImageView mIvStatus;
    private TextView mTvStatus;
    private TextView mTvTime;
    private TextView mTvAmountValue;
    private TextView tvGas;
    private TextView mTvGasValue;
//    private TextView mTvGasFormula;
    private TextView mTvReceiveAddress;
    private TextView mTvPayAddress;
    private TextView mTvNoteValue;
    private TextView mTvNumberValue;
    private TextView tvBlock;
    private TextView mTvBlockValue;
    private ImageView mIvBarCode;
    private TransactionBean transactionBean;
    private int chainId;

    private String queryUrl =
            "https://etherscan.io/tx/0xd5f72f8f08b06cc8cf1a1346e19dcc9d2fe00c7a611dd623a6329415765a3ff9";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_transaction_detail);
        initView();

    }

//    /**
//     *
//     * @param type 类型，TRANSACTION_TYPE_OUT, TRANSACTION_TYPE_IN转出和转入
//     * @param success 是否转账成功
//     * @param time 时间
//     * @param amount 转账数量
//     * @param gas 矿工费用
//     * @param receiveAddress 对方的地址
//     * @param payAddress 自己的地址
//     * @param txNumber 交易号
//     */
//    public static void start(Context context, int type, boolean success, String time, String amount,
//                             String gas, String receiveAddress, String payAddress, String txNumber,
//                            int block) {
//        Intent starter = new Intent(context, TransactionDetailActivity.class);
//        starter.putExtra("type", type);
//        starter.putExtra("success", success);
//        starter.putExtra("time", time);
//        starter.putExtra("amount", amount);
//        starter.putExtra("gas", gas);
//        starter.putExtra("receiveAddress", receiveAddress);
//        starter.putExtra("payAddress", payAddress);
//        starter.putExtra("txNumber", txNumber);
//        starter.putExtra("block", block);
//        context.startActivity(starter);
//    }
    public static void start(Context context, TransactionBean bean) {
        Intent starter = new Intent(context, TransactionDetailActivity.class);
        starter.putExtra("transactionBean", bean);
        context.startActivity(starter);
    }
    private void initView() {
        transactionBean = (TransactionBean) getIntent().getSerializableExtra("transactionBean");
        chainId = transactionBean.getChainId();
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.open_planet_asset_trade_line_details));
        String status = "";

        mIvStatus = (ImageView) findViewById(R.id.iv_status);
        if (transactionBean.getTransactionStatus()) {
            status = ResStringUtil.getString(R.string.common_text_success);
            mIvStatus.setImageDrawable(getResources().getDrawable(R.drawable.transfer_success));
        } else {
            status =  ResStringUtil.getString(R.string.common_text_fails);
            mIvStatus.setImageDrawable(getResources().getDrawable(R.drawable.transfer_fail));
        }

        mTvTime = (TextView) findViewById(R.id.tv_time);
        mTvTime.setText(transactionBean.getTime());

        mTvAmountValue = (TextView) findViewById(R.id.tv_amount_value);
        mTvAmountValue.setText(transactionBean.getAmount());

        tvGas = (TextView) findViewById(R.id.tv_gas);
        mTvGasValue = (TextView) findViewById(R.id.tv_gas_value);
        mTvGasValue.setText(transactionBean.getGas());

//        mTvGasFormula = (TextView) findViewById(R.id.tv_gas_formula);
        mTvReceiveAddress = (TextView) findViewById(R.id.tv_receive_address);
        mTvReceiveAddress.setOnClickListener(this);

        mTvPayAddress = (TextView) findViewById(R.id.tv_pay_address);
        mTvPayAddress.setOnClickListener(this);
        mTvNoteValue = (TextView) findViewById(R.id.tv_note_value);

        mTvNumberValue = (TextView) findViewById(R.id.tv_number_value);

        String txNumber =transactionBean.getTransactionNumber();
        if (!StringUtils.isEmpty(txNumber) && txNumber.length() > 10) {
            String formatTxNumber = txNumber.replace(txNumber.substring(6, txNumber.length() - 4), "****");
            mTvNumberValue.setText(formatTxNumber);
        } else {
            mTvNumberValue.setText(txNumber);
        }
        tvBlock = (TextView) findViewById(R.id.tv_block);
        mTvBlockValue = (TextView) findViewById(R.id.tv_block_value);
        mTvBlockValue.setText(transactionBean.getBlock()+"");
        mIvBarCode = (ImageView) findViewById(R.id.iv_bar_code);

        String receiveAddress;
        String payAddress;
        mTvStatus = (TextView) findViewById(R.id.tv_status);
        if (transactionBean.getTransactionType() == TRANSACTION_TYPE_OUT) {
            //转出
            mTvStatus.setText(ResStringUtil.getString(R.string.open_planet_asset_trade_line_transfer_out) + status);
        } else {
            //转入
            mTvStatus.setText(ResStringUtil.getString(R.string.open_planet_asset_trade_line_transfer_in) + status);
        }

        mTvReceiveAddress.setText(divideAddress(transactionBean.getToAddress()));
        mTvPayAddress.setText(divideAddress(transactionBean.getFromAddress()));

        Bitmap bm = QRCode.setQRcode(queryUrl, 800, 800);
        if (bm != null) {
            mIvBarCode.setImageBitmap(bm);
        }
        if (chainId == EosNetParamUtil.getEosChainId()) {
            tvGas.setVisibility(View.GONE);
            mTvGasValue.setVisibility(View.GONE);
            tvBlock.setText(ResStringUtil.getString(R.string.open_planet_asset_trade_line_contract));
        }
        mTvNoteValue.setText(transactionBean.getNote());
    }

    /**
     * 将地址分为两行显示
     * @param address 地址
     * @return address
     */
    private String divideAddress(String address) {
        if (address.equals("")) {
            return "";
        }
        if (address.length() < 16) {
            return address;
        }
        return address.substring(0, address.length() / 2) +"\n"
                +address.substring(address.length() / 2, address.length());
    }

    private void copyAddress(String address) {
        //复制账号
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(address);
        ToastUtils.setBgColor(getResources().getColor(R.color.black));
        ToastUtils.setMsgColor(getResources().getColor(R.color.op_text_white_color));
        ToastUtils.showShort(ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_base_id_copied));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_receive_address) {
            copyAddress(mTvReceiveAddress.getText().toString().replaceAll("\n",""));
        } else if (id == R.id.tv_pay_address) {
            copyAddress(mTvPayAddress.getText().toString().replaceAll("\n",""));
        }
    }
}
